﻿/* AppPart.js ---------------------------------------------------------------
 *
 * Informatix Football Apps
 * QB Passing Efficiency Calculator (SharePoint App Part)
 *
 * © 2013 Jason Barkes - http://jbarkes.blogspot.com
 ----------------------------------------------------------------------------*/

/* This function is executed after the DOM is ready and SharePoint scripts are loaded.
-----------------------------------------------------------------------------*/
function sharePointReady() {

    /* Select input control text on select - same as tab behavior */
    $("input[type=text], textarea").focus(function () {
        this.select();
    });
}

/* This function retrieves a query string value from a URL.
-----------------------------------------------------------------------------*/
function getQueryStringParam(param) {
    var strParams = "";
    var params = document.URL.split("?")[1].split("&");

    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == param) {
            return singleParam[1];
        }
    }
}

/* This function verifies the passing stats entered by the user.
-----------------------------------------------------------------------------*/
function verify_stats(comps, atts, yards, tds, ints) {
    if (!(comps) || (comps == "") || isNaN(comps)) {
        alert('Please specify the number of passing completions.');
        $('#ctl_completions').focus();
        return false;
    }
    if (!(atts) || (atts == "") || isNaN(atts) || atts <= 0) {
        alert('Please specify the number of passing attempts.');
        $('#ctl_attempts').focus();
        return false;
    }
    if (!(yards) || (yards == "") || isNaN(yards)) {
        alert('Please specify the number of passing yards.');
        $('#ctl_yards').focus();
        return false;
    }
    if (!(tds) || (tds == "") || isNaN(tds)) {
        alert('Please specify the number of passing TDs.');
        $('#ctl_tds').focus();
        return false;
    }
    if (!(ints) || (ints == "") || isNaN(ints)) {
        alert('Please specify the number of interceptions.');
        $('#ctl_ints').focus();
        return false;
    }
    if (parseInt(atts) < parseInt(comps)) {
        alert('Attempts must be greater than or equal to the number of completions.');
        $('#ctl_attempts').focus();
        return false;
    }
    if (parseInt(atts) < (parseInt(comps) + parseInt(ints))) {
        alert('Attempts must be greater than or equal to the total number of completions and interceptions.');
        $('#ctl_attempts').focus();
        return false;
    }
    if (parseInt(comps) < parseInt(tds)) {
        alert('Completions must be greater than or equal to the number of passing TDs.');
        $('#ctl_completions').focus();
        return false;
    }

    return true;
}

/* This function resets the input calculation controls.
-----------------------------------------------------------------------------*/
function calc_reset() {
    $('#ctl_completions').val('');
    $('#ctl_attempts').val('');
    $('#ctl_yards').val('');
    $('#ctl_tds').val('');
    $('#ctl_ints').val('');
    $('#ctl_ncaaRating').val('');
    $('#ctl_nflRating').val('');

    $('#ctl_completions').focus();
}

/* This function calculates the NFL and NCAA QB passer efficiency ratings
   and displays the results.
-----------------------------------------------------------------------------*/
function calc_ratings() {
    var comps = $('#ctl_completions').val(),
        atts = $('#ctl_attempts').val(),
        yards = $('#ctl_yards').val(),
        tds = $('#ctl_tds').val(),
        ints = $('#ctl_ints').val();
    var cp, ya, tp, ip;
    var nfl_cpc, nfl_yac, nfl_tpc, nfl_ipc, nfl_cpa, nfl_yaa, nfl_tpa, nfl_ipa;
    var ncaa_cpc, ncaa_yac, ncaa_tpc, ncaa_ipc;
    var adj_rating, nfl_rating, ncaa_rating;

    /* Verify passing stats */
    if (verify_stats(comps, atts, yards, tds, ints) == false) {
        return false;
    }

    /* Completion Percentage (adjusted NFL value must be between 0 and 2.375)
    -------------------------------------------------------------------------*/
    cp = (comps / atts) * 100;
    nfl_cpa = nfl_cpc = (cp - 30) * 0.05;
    if (nfl_cpc < 0) {
        nfl_cpa = 0
    }
    else if (nfl_cpc > 2.375) {
        nfl_cpa = 2.375;
    }
    ncaa_cpc = cp;

    /* Yards per Attempt (adjusted NFL value must be between 0 and 2.375)
    -------------------------------------------------------------------------*/
    ya = yards / atts;
    nfl_yaa = nfl_yac = (ya - 3) * 0.25;
    if (nfl_yac < 0) {
        nfl_yaa = 0
    }
    else if (nfl_yac > 2.375) {
        nfl_yaa = 2.375;
    }

    ncaa_yac = ya * 8.4;

    /* TD Percentage (adjusted NFL value must be between 0 and 2.375)
    -------------------------------------------------------------------------*/
    tp = (tds / atts) * 100;
    nfl_tpa = nfl_tpc = (tp * 0.2);
    if (nfl_tpc > 2.375) {
        nfl_tpa = 2.375;
    }
    ncaa_tpc = tp * 3.3;

    /* Interception Percentage (adjusted NFL value must be between 0 and 2.375)
    -------------------------------------------------------------------------*/
    ip = (ints / atts) * 100;
    nfl_ipa = nfl_ipc = 2.375 - (ip * 0.25);
    if (nfl_ipc < 0) {
        nfl_ipa = 0;
    }
    ncaa_ipc = ip * 2;

    /* Final calculations
    -------------------------------------------------------------------------*/

    /* Final NFL rating */
    adj_rating = (nfl_cpa + nfl_yaa + nfl_tpa + nfl_ipa) / 6;
    nfl_rating = adj_rating * 100;
    $('#ctl_nflRating').val(nfl_rating.toFixed(1));

    /* Final NCAA rating */
    ncaa_rating = ncaa_cpc + ncaa_yac + ncaa_tpc - ncaa_ipc;
    $('#ctl_ncaaRating').val(ncaa_rating.toFixed(1));
}
